
package planet.login;

import java.io.IOException;
import java.util.Collection;
import java.util.Iterator;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.swing.JOptionPane;
import planet.bean.BeanUsuario;

import comun.DAOFactory;
import comun.Usuario;

public class ServletLogin extends HttpServlet {

	public void doPost(HttpServletRequest request,HttpServletResponse response)
	throws ServletException, IOException {
		DAOFactory objDAOFact=DAOFactory.getDAOFactory(DAOFactory.ORACLE);
		Usuario objUD=objDAOFact.getUsuario();
		
		
	
	String codigo=request.getParameter("txt_Usuario");
	String clave=request.getParameter("txt_Clave");
	System.out.println("codigo "+codigo+"-");
	System.out.println("clave "+clave+"-");
	
	
	BeanUsuario objUB=null;
	try{
		objUB=objUD.validarUsuario(codigo, clave);		   
		 
	}catch(Exception e){
		System.out.println("ERROR en service de ServletLogin: "+e.getMessage());
	}
	if(objUB!=null){
		if (request.isRequestedSessionIdValid()) {
			HttpSession tempSession = request.getSession(false);
			tempSession.invalidate();
			System.out.println("la sesion estaba activa");
		}
		HttpSession session=request.getSession(true);
		
		
		session.setAttribute("cod_usu",objUB.getCOD_USUARIO());			
		session.setAttribute("clave_usu",objUB.getCLAVE_USUARIO());
		session.setAttribute("nom_usu",objUB.getNOM_USUARIO()); 	
		
		getServletContext().getRequestDispatcher("/cineplanet/login/frm_cineplanet_menu_principal.jsp").forward(request,response);
		
	}else{
		getServletContext().getRequestDispatcher("/cineplanet/login/frm_cineplanet_login.jsp").forward(request,response);
	}
}
}
